package cn.zh.component;

import cn.zh.entity.User;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.stereotype.Component;
import java.io.IOException;

@Component
public class MqListener {

//    @RabbitListener(queues = "directQueue-One")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "directQueue-One",durable = "false"),
            exchange = @Exchange(value = "MqSendService-One",type = "direct", durable = "false"),
            key = "One"
    ))
    public void receiveDirectMsg(Message data) {
        //properties内有消息队列等信息
        MessageProperties properties = data.getMessageProperties();
        //得到消息体，变为String输出
        User o = JSON.parseObject(data.getBody(), User.class);
        System.out.println(o);
        String str = new String(data.getBody());
        System.out.println(str);
    }
}
